const express = require("express");
const http = require("http");
const cors = require("cors");
const bodyParser = require("body-parser");
const indexRouter = require("../routes/index");

const serverPort = 5555;

//1. 创建web服务
const app = express();
const server = http.createServer(app);   //既支持websocket又支持restfulAPI
app.use(cors());                         //允许跨域
require("./websocket")(server);

//2. 配置中间件
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

//3. 挂在路由
app.use("/", indexRouter);

//4. 监听服务器端口
server.listen(serverPort, () => {
  console.log("server is running on http://127.0.0.1:" + serverPort);
});
